package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.device.pojo.MesDeviceConsumable;
import cn.darkhorse.device.pojo.MesDeviceConsumableType;
import cn.darkhorse.device.service.MesDeviceConsumableService;
import cn.darkhorse.device.service.MesDeviceConsumableTypeService;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 耗材管理 前端控制器
 * </p>
 *
 * @author yangtianqi
 * @since 2024-11-06
 */
@RestController
@RequestMapping("/device/mesDeviceConsumable")
@Api(tags = "耗材管理")
public class MesDeviceConsumableController extends BaseController {
    @Autowired
    private MesDeviceConsumableService mesDeviceConsumableService;
    @Autowired
    private MesDeviceConsumableTypeService mesDeviceConsumableTypeService;
    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 获取耗材类型编码
     * @return
     */
    @ApiOperation(value = "获取编码")
    @GetMapping("/getCode")
    public AjaxResult getCode() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",System.currentTimeMillis());
        return AjaxResult.success(jsonObject);
    }

    @ApiOperation(value = "删除耗材")
    @GetMapping("/del")
    public AjaxResult delMesDeviceConsumable(Long id) {
        boolean result = mesDeviceConsumableService.delete(id);
        return result ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation(value = "获取耗材列表")
    @GetMapping("/list")
    public TableDataInfo list(MesDeviceConsumable mesDeviceConsumable){
        startPage();
        List<MesDeviceConsumable> list = mesDeviceConsumableService.list(mesDeviceConsumable);
        for (MesDeviceConsumable consumable : list) {
            MesDeviceConsumableType byId = mesDeviceConsumableTypeService.getById(consumable.getType());
            if (byId != null){
                consumable.setTypeName(byId.getTypeName());
            }
            if (consumable.getUnit() != null){
                String s = dictDataService.selectDictLabel("unit_type", consumable.getUnit());
                consumable.setUnitName(s);
            }

        }
        return getDataTable(list);
    }

    @ApiOperation(value = "添加或修改耗材")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody MesDeviceConsumable mesDeviceConsumable) {
        System.out.println(mesDeviceConsumable.getId());
        boolean result = mesDeviceConsumableService.saveMesDeviceConsumable(mesDeviceConsumable);
        return result ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation(value = "根据耗材Id获取耗材信息")
    @GetMapping("/view")
    public AjaxResult getMesDeviceConsumable(Long id) {
        MesDeviceConsumable consumable = mesDeviceConsumableService.view(id);
        if (consumable != null){
            MesDeviceConsumableType byId = mesDeviceConsumableTypeService.getById(consumable.getType());
            if (byId != null){
                consumable.setTypeName(byId.getTypeName());
            }
            if (consumable.getUnit() != null){
                String s = dictDataService.selectDictLabel("unit_type", consumable.getUnit());
                consumable.setUnitName(s);
            }
        }
        return AjaxResult.success(consumable);
    }



}

